package com.synnex.mobile.servier.core.survey.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.survey.bo.SurveyAnswerBo;
import com.synnex.mobile.servier.core.survey.dao.SurveyAnswerDao;
import com.synnex.mobile.servier.core.survey.dto.SurveyAnswer;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-12
 * To change this template use File | Settings | File Templates.
 */
public class SurveyAnswerDaoImpl extends GenericHibernateDaoImpl<SurveyAnswer,Integer> implements SurveyAnswerDao {

    public void saveSurveyAnswer(SurveyAnswer surveyAnswer) {
        super.save(surveyAnswer);
    }

    public List<SurveyAnswer> listSABySurveyId(Integer surveyId) {
        Criteria c  = this.getSession().createCriteria(SurveyAnswer.class);
        c.add(Restrictions.eq("sId",surveyId));

        return c.list();
    }

    public  List<SurveyAnswerBo> listSACountBySid(Integer surveyId) {
        String sql="SELECT COUNT(sa.a_id),sa.s_id ,sa.a_id FROM question_survey_answer sa WHERE sa.s_id=:sid and sa.a_id is not null GROUP BY sa.a_id";

        List<Object[]> datas =  this.getSession().createSQLQuery(sql)
                .setParameter("sid",surveyId).list();

        List<SurveyAnswerBo> saCounts = new ArrayList<SurveyAnswerBo>();

        if(datas!=null && !datas.isEmpty()){
            for(Object[] obj : datas){
                SurveyAnswerBo saBo = new SurveyAnswerBo();

                BigInteger bigCount = (BigInteger)obj[0];
                saBo.setCount(bigCount.intValue());

                saBo.setSid((Integer)obj[1]);
                saBo.setAid((Integer)obj[2]);
                saCounts.add(saBo);
            }
        }
        return   saCounts;
    }


    public List<SurveyAnswer> getAskAnswerBySid(Integer sid){
        Criteria c  = this.getSession().createCriteria(SurveyAnswer.class);
        c.add(Restrictions.eq("sId",sid));
        c.add(Restrictions.isNotNull("aText"));

        return c.list();

    }
}
